{"cells": [{"cell_type": "markdown", "metadata": {}, "source": "# 06 Profiler\nKernel Profiler \u6027\u80fd\u5206\u6790\u793a\u4f8b"}, {"cell_type": "code", "metadata": {}, "source": "import torch\nimport triton\nimport triton.language as tl\nfrom torch.profiler import profile, ProfilerActivity\n\n@triton.jit\ndef vector_add_kernel(X_ptr, Y_ptr, Z_ptr, N: tl.constexpr):\n    pid = tl.program_id(0)\n    if pid < N:\n        tl.store(Z_ptr + pid, tl.load(X_ptr + pid) + tl.load(Y_ptr + pid))\n\nN = 1024\nx = torch.randn(N, device='cuda')\ny = torch.randn(N, device='cuda')\nz = torch.empty_like(x)\n\nwith profile(activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA], record_shapes=True) as prof:\n    vector_add_kernel[(N,)](x, y, z, N=N)\n\nprint(\"z[:5] =\", z[:5])\nprint(prof.key_averages().table(sort_by=\"cuda_time_total\"))", "outputs": [], "execution_count": null}], "metadata": {"title": "Profiler"}, "nbformat": 4, "nbformat_minor": 5}